package gminet.infra.dao;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.sql.Types;
import java.util.Date;

public abstract class DataAccessObject {

    protected static float getFloat(ResultSet rs, String nomeCampo) throws SQLException {

        return rs.getFloat(nomeCampo);
    }

    protected static int getInt(ResultSet rs, String nomeCampo) throws SQLException {
        
        return rs.getInt(nomeCampo);
    }

    protected static byte getByte(ResultSet rs, String nomeCampo) throws SQLException {
        
        return rs.getByte(nomeCampo);
    }

    protected static Date getDate(ResultSet rs, String nomeCampo) throws SQLException {

        java.sql.Date sqlDate = rs.getDate(nomeCampo);
        Date date = null;
        if (sqlDate != null) {
            date = new Date(sqlDate.getTime());
        }
        return date;
    }

    protected static Date getDateFromTimestamp(ResultSet rs, String nomeCampo) throws SQLException {

        Timestamp ts = rs.getTimestamp(nomeCampo);
        Date date = null;
        if (ts != null) {
            date = new Date(ts.getTime());
        }
        return date;
    }

    protected static String getString(ResultSet rs, String nomeCampo) throws SQLException {
        
        String nome = rs.getString(nomeCampo);
        if (nome == null) {
            return "";
        } else
            return nome;
    }

    protected static char getChar(ResultSet rs, String nomeCampo) throws SQLException {
        
        String ch = rs.getString(nomeCampo);
        if (ch == null) {
            return ' ';
        } else
            return ch.trim().charAt(0);
    }

    protected static void setInt(PreparedStatement ps, int i, int valor) throws SQLException {

        setInt(ps, i, valor, false);
    }

    protected static void setInt(PreparedStatement ps, int i, int valor, boolean zeroIsNull)
            throws SQLException {

        if (zeroIsNull && valor == 0) {
            ps.setNull(i, Types.INTEGER);
        } else {
            ps.setInt(i, valor);
        }
    }

    protected static void setByte(PreparedStatement ps, int i, byte valor) throws SQLException {

        ps.setByte(i, valor);
    }

    protected static void setString(PreparedStatement ps, int i, String valor) throws SQLException {

        setString(ps, i, valor, true);
    }

    protected static void setString(PreparedStatement ps, int i, String valor, boolean blankIsNull)
            throws SQLException {

        if (valor == null || (valor.equals("") && blankIsNull)) {
            ps.setNull(i, Types.VARCHAR);
        } else {
            ps.setString(i, valor);
        }
    }

    protected static void setTimestamp(PreparedStatement ps, int i, Date valor) throws SQLException {

        if (valor != null) {
            ps.setTimestamp(i, new Timestamp(valor.getTime()));
        } else
            ps.setNull(i, Types.TIMESTAMP);
    }

    protected static void setDate(PreparedStatement ps, int i, Date valor) throws SQLException {
        
        if (valor != null) {
            ps.setDate(i, new java.sql.Date(valor.getTime()));
        } else {
            ps.setNull(i, Types.DATE);
        }
    }

    protected static void setDouble(PreparedStatement ps, int i, double valor) throws SQLException {
        
        if (!Double.isNaN(valor) && !Double.isInfinite(valor)) { // ||
            ps.setDouble(i, valor);
        } else {
            ps.setDouble(i, Types.FLOAT);
        }
    }

    protected static void setFloat(PreparedStatement ps, int i, float valor) throws SQLException {
        
        if (!Float.isNaN(valor) && !Float.isInfinite(valor)) { // ||
            ps.setFloat(i, valor);
        } else {
            ps.setNull(i, Types.FLOAT);
        }
    }

    protected static void setFloat(PreparedStatement ps, int i, float valor, boolean zeroIsNull)
            throws SQLException {

        if (zeroIsNull && valor == 0) {
            ps.setNull(i, Types.FLOAT);
        } else if (!Float.isNaN(valor) && !Float.isInfinite(valor)) {
            ps.setFloat(i, valor);
        } else {
            ps.setNull(i, Types.FLOAT);
        }
    }

    public static void close(PreparedStatement ps) throws SQLException {

        if (ps != null) {
            ps.close();
        }
        ps = null;
    }

    public static void close(ResultSet rs) throws SQLException {

        if (rs != null) {
            rs.close();
        }
        rs = null;
    }

}
